<?php
/**
 * Widget.php
 * 
 * 部件(widget)基类
 * @author wang <wang@fvzone.com>
 * @copyright  Copyright (c) 2010 Fire vision Software Studio
 * @link http://www.fvzone.com
 * @license New BSD License.{@link http://www.opensource.org/licenses/bsd-license.php}
 * @version $Id: Widget.php 1.0 2010-11-3 22:02:01Z wang $
 * @package core
 * @since 1.0
 */

defined('IN_WORK') or exit('Authorization error');

abstract class Widget extends Controller
{
	/**
	 * 运行部件抽象方法
	 *
	 * @return mixed
	 */
	abstract public function render();

	/**
	 * 分析挂件(widget)的视图文件路径
	 * 
	 * 注:这里的视图指的是挂件的视图文件
	 * @access public
	 * @param string $file_name	视图文件名.注:不含文件后缀
	 * @return string			文件路径
	 */
	public function template($name = null, $suffix = '.php')
    {
        //部件主题文件名称
        $filename = 'widgets/views/'.(is_null($name) ? $this->getName() : $name). '.php';

        //当前组件里找视图文件
        $view_file = COMPONENT_DIR. Request::component(). DS .$filename;
        //应用里找视图文件
        if (!is_file($view_file))
        {
            $view_file = PUBLIC_DIR.$filename;
        }

		return $view_file;
	}

	/**
	 * 获取当前部件名称
	 * 
	 * @return string
	 */
    public function getName()
    {
        return substr(strtolower(get_class($this)), 0, -7);
    }
	
	/**
	 * 视图类的实例化
	 * 
	 * 注:视图文件仅限于HTML格式的
	 * @access public
	 * @return boolean
	 */
	public function view()
    {
        //关闭跟踪信息
        Work::config()->SHOW_TRACE = false;

        $this->view = new View();

		$this->view->widget		  = $this->getName();
        
        $this->view->view_dir     = PUBLIC_DIR . 'widgets/views' . DS;
        $this->view->compile_dir  = CACHE_DIR .'components/'. Request::component() .'/widgets/compile' .DS;
        $this->view->cache_dir    = CACHE_DIR .'components/'. Request::component() .'/widgets/cache' .DS;
	}

}